<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017-9-28
 * Time: 19:31
 */

Yaf_Loader::import(CONTROLLERS_PATH.'Controller.php');
Yaf_Loader::import(EXTEND_PATH.'/qmessage/Messages.php');
Yaf_Loader::import(EXTEND_PATH.'/qvideo/Videos.php');

class LiveController extends Controller
{
    public function init()
    {
        $this->session = Yaf_Session::getInstance();
    }

    public function indexAction()
    {
        $llid=$this->getRequest()->get("llid");
        $info = Db::name('live_list')
            ->field('ll.id as llid,ll.name,if(ll.password,1,0) as password,ll.share,ll.status,ll.play,ll.viewing,ud.nickname,ud.avatar')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->where(['ll.id'=>$llid])
            ->find();
        if($info['share']){
            if($info['status'] == 2){
                $this->redirect('live?llid='.$llid);
                exit;
            }
            if($info['status'] == 9){
                $this->redirect('back?llid='.$llid);
                exit;
            }
        }
        $this->getView()->assign("info", $info);
    }

    public function backAction()
    {
        $llid=$this->getRequest()->get("llid");
        $info = Db::name('live_list')
            ->field('ll.id as llid,if(ll.password,1,0) as password,ll.share,ll.view,ll.viewing,ll.status,ll.annex,ll.image,ll.video,ud.nickname,ud.avatar')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->where(['ll.id'=>$llid])
            ->find();
        Db::name('live_list')->where(['id'=>$llid])->setInc('viewing');
        if($info['status'] != 9 || !$info['share']){
            $this->redirect('index?llid='.$llid);
            exit;
        }
        if($info['view']){
            $info['annex'] = json_decode($info['annex'],true);
        }else{
            $info['annex'] = [];
        }
        $video = new Videos();
        $url = $video->getVideoInfo($info['video']);
        $this->getView()->assign("url", $url);
        $this->getView()->assign("info", $info);
    }

    public function liveAction()
    {
        $llid=$this->getRequest()->get("llid");
        $info = Db::name('live_list')
            ->field('ll.id as llid,ll.name,if(ll.password,1,0) as password,ll.share,ll.view,ll.play,ll.click,ll.viewing,ll.channel,ll.status,ll.annex,ll.image,ll.video,ud.nickname,ud.avatar')->alias('ll')
            ->join('users_detail ud','ll.udid = ud.id')
            ->where(['ll.id'=>$llid])
            ->find();
        if($info['status'] != 2 || !$info['share']){
            $this->redirect('index?llid='.$llid);
            exit;
        }
        $play = Db::name('live_list')->field('SEC_TO_TIME(TIMESTAMPDIFF(SECOND,play_time,NOW())) as play')->where(['id' => $llid])->find();
        $info['play'] = $play['play'];
        $url = $this->getPlayUrl($info['channel']);
        if(!$this->session->user){
            Db::name('live_list')->where(['id'=>$info['llid']])->inc('click')->setInc('viewing');
            $user['sdkAppID'] = 1400043256;
            $user['accountType'] = 17845;
            $udid = mt_rand(100000000, 999999999);
            $user['nickname'] = '学员'.substr($udid,-4);
            $user['udid'] = $udid;
            $user['avatar'] = 'http://train.img.livestaring.com/log.png';
            $message = new Messages();
            $user['tim'] = $message->usrsig($user['udid']);
            $message->account_import([(string)$user['udid'],$user['nickname'],$user['avatar']]);
            $this->session->user=$user;
        }else{
            $user = $this->session->user;
        }

        if($info['view']){
            $info['annex'] = json_decode($info['annex'],true);
        }else{
            $info['annex'] = [];
        }
        $this->getView()->assign("url", $url);
        $this->getView()->assign("user", $user);
        $this->getView()->assign("info", $info);
    }

    public function passwordAction()
    {
        if($this->getRequest()->isPost()){
            $content = $this->getRequest()->getPost();
            $validate = new Validate([
                'llid' => 'require|number|egt:1|exist:live_list,id',
                'password' => 'require|number|length:6|between:100000,999999',
            ]);
            $validate->message([
                'llid.require' => '课程不能为空',
                'llid.number' => '课程类型错误',
                'llid.egt' => '课程错误',
                'llid.exist' => '课程不存在',
                'password.require' => '密码不能为空',
                'password.number' => '密码错误',
                'password.length' => '密码错误',
                'password.between' => '密码错误',
            ]);
            if ($validate->check($content)) {
                if(!Db::name('live_list')->where(['id'=>$content['llid'],'password'=>$content['password'],'status'=>['in','2,9']])->find()){
                    $code = 400;
                    $data['message'] = '密码错误';
                }else{
                    $code = 200;
                    $data['message'] = '验证通过';
                }
            }else{
                $code = 400;
                $data['message'] = $validate->getError();
            }
        }else{
            $code = 500;
            $data['message'] = '请求错误';
            $content = $this->getRequest()->getRequest();
        }
        $this->returnApi($data,$code,$content);
    }
    /**
     * 获取播放地址
     * @param int $bizId 您在腾讯云分配到的bizid
     * @param String $streamId 您用来区别不通推流地址的唯一id
     * @return String url
     */
    public function getPlayUrl($streamId,$bizId =5853)
    {
        $livecode = $bizId.'_'.$streamId; //直播码
        $url = [
            'rtmp://'.$bizId.'.liveplay.myqcloud.com/live/'.$livecode,
            'https://'.$bizId.'.liveplay.myqcloud.com/live/'.$livecode.'.flv',
            'https://'.$bizId.'.liveplay.myqcloud.com/live/'.$livecode.'.m3u8'
        ];
        return $url;
    }

}
